package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Product;
import ysq.xyg.model.info.ProductInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ProductService;
import ysq.xyg.service.WebCatalogService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 文章
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ProductController.class);
    @Resource
    private ProductService productService;
    @Resource
    private WebCatalogService webCatalogService;
    private Product product;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/productList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Product product, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Product> pageInfo = productService.pageInfo(product, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/productUpdateOrSave");
        product = productService.selectByPrimaryKey(id);
        view.addObject("product", product);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(ProductInfo productInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(productInfo.getId())) {
            product = new Product();
            product.setId(BaseUtil.uId());
            product.setTitle(productInfo.getTitle());
            product.setDescription(productInfo.getDescription());
            product.setContent(productInfo.getContent());
            product.setType(productInfo.getType());
            product.setOrgDesc(productInfo.getOrgDesc());
            product.setNotice(productInfo.getNotice());
            product.setMoney(productInfo.getMoney());
            product.setStatus(productInfo.getStatus());
            product.setPayMoney(productInfo.getPayMoney());
            product.setProductPic(productInfo.getProductPic());
            product.setSort(productInfo.getSort());
            product.setNum(productInfo.getNum());
            product.setShowPic(productInfo.getShowPic());
            product.setCreateTime(new Date());
            int id = productService.insert(product);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            product = productService.selectByPrimaryKey(productInfo.getId());
            product.setTitle(productInfo.getTitle());
            product.setType(productInfo.getType());
            product.setMoney(productInfo.getMoney());
            product.setProductPic(productInfo.getProductPic());
            product.setOrgDesc(productInfo.getOrgDesc());
            product.setPayMoney(productInfo.getPayMoney());
            product.setNotice(productInfo.getNotice());
            product.setContent(productInfo.getContent());
            product.setStatus(productInfo.getStatus());
            product.setDescription(productInfo.getDescription());
            product.setSort(productInfo.getSort());
            product.setNum(productInfo.getNum());
            product.setShowPic(productInfo.getShowPic());
            int id = productService.updateByPrimaryKey(product);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = productService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
